package com.ibeetl.admin.console.web;

import com.ibeetl.admin.api.entity.Travel_attractions_line;
import com.ibeetl.admin.api.entity.Travel_attractions_line_info;
import com.ibeetl.admin.api.entity.Travel_guide;
import com.ibeetl.admin.api.entity.Travel_user;
import com.ibeetl.admin.api.service.FileService;
import com.ibeetl.admin.api.service.TravelAttractionsLineService;
import com.ibeetl.admin.api.service.TravelGuideService;
import com.ibeetl.admin.api.service.UserService;
import com.ibeetl.admin.api.util.UrlUtils;
import com.ibeetl.admin.api.web.query.TravelGuideQuery;
import com.ibeetl.admin.api.web.query.TravelUserQuery;
import com.ibeetl.admin.core.web.JsonResult;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
public class TravelController {

    private static final String TRAVEL_ATTRACTIONS_LINE = "/web/travel_attractions_line";
    private static final String TRAVEL_USER = "/web/travelUser";
    private static final String TRAVEL_GUIDE = "/web/travel_guide";

    @Resource
    private TravelAttractionsLineService travelAttractionsLineService;
    @Resource
    private FileService fileService;
    @Resource
    private UserService userService;
    @Resource
    private TravelGuideService travelGuideService;


    @GetMapping(TRAVEL_ATTRACTIONS_LINE + "/audit.do")
    @CrossOrigin
    public ModelAndView queryDataById(Long id) {
        Travel_attractions_line travel_attractions_line  = travelAttractionsLineService.queryById(id);
        ModelAndView view = new ModelAndView("/admin/attractionsLine/audit.html");
        view.addObject("travelAttractionsLine", travel_attractions_line);
        return view;
    }

    /**
     * 文件预览接口
     */
    @GetMapping(value = "/file/review")
    public ResponseEntity<?> pdfReview(@Param("fileName") String fileName) throws IOException {
        return fileService.downloadFile(UrlUtils.URLDecoder(fileName));

    }

    @GetMapping(TRAVEL_ATTRACTIONS_LINE + "/queryTravelAttractionsLineInfo.json")
    @CrossOrigin
    public JsonResult<List> queryTravelAttractionsLineInfo(Long attractionId) {
        List<Travel_attractions_line_info>  travel_attractions_line_infos  = travelAttractionsLineService.queryTravelAttractionsLineInfo(attractionId);
        return JsonResult.success(travel_attractions_line_infos);
    }


    @GetMapping(TRAVEL_USER + "/index.do")
    @ResponseBody
    public ModelAndView travelUser() {
        ModelAndView view = new ModelAndView("/admin/travelUser/index.html") ;
        view.addObject("search", TravelUserQuery.class.getName());
        return view;
    }


    @GetMapping(TRAVEL_USER + "/edit.do")
    @ResponseBody
    public ModelAndView edit(Integer userId) {
        ModelAndView view = new ModelAndView("/admin/travelUser/edit.html");
        Travel_user travelUser = userService.queryById(userId);
        view.addObject("travelUser", travelUser);
        return view;
    }

    @GetMapping(TRAVEL_USER + "/add.do")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/admin/travelUser/add.html");
        return view;
    }

    @GetMapping(TRAVEL_GUIDE + "/index.do")
    @ResponseBody
    public ModelAndView travelGuide() {
        ModelAndView view = new ModelAndView("/admin/travelGuide/index.html") ;
        view.addObject("search", TravelGuideQuery.class.getName());
        return view;
    }
    @GetMapping(TRAVEL_GUIDE + "/audit.do")
    @ResponseBody
    public ModelAndView audit(Integer guideId) {
        ModelAndView view = new ModelAndView("/admin/travelGuide/audit.html");
        Travel_guide travelGuide = travelGuideService.queryById(guideId);
        view.addObject("travelGuide", travelGuide);
        return view;
    }

}
